<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="item.id">
					<uni-card :title="'产成品序号:'+(index+1)">
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<text class="uni-body">数量：{{ item.qty }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>
		
		<view class="footer">
			<button type="default" @click="print">重新打印临时托码</button>
		</view>
	</view>
</template>

<script>
	import { getToken } from '@/utils/auth'
	const printer = uni.requireNativePlugin('LcPrinter');
	const modal = uni.requireNativePlugin('modal');
	var globalEvent = uni.requireNativePlugin('globalEvent');
	export default {
		data() {
			return {
				productList:[],
				baseUrl:getApp().globalData.config.baseUrl,
				userName:this.$store.state.user.name,
				orgId:getApp().globalData.config.orgId,
				barcodeContent:'',
				message:'',
				date:'',
			}
		},
		
		onLoad: function (option){	
			this.orgId = uni.getStorageSync('orgId');
			// 添加打印状态监听
			globalEvent.addEventListener('onPrintCallback', function(e) {
				// uni.showToast({
				// 	title: 'state: ' + JSON.stringify(e),
				// 	duration: 2000
				// });
				if (e.key == 0) {
					uni.showToast({
						title: '打印成功',
						duration: 2000
					});
				} else if (e.key == 3) {
					uni.showToast({
						title: '缺纸',
						duration: 2000
					});
				}
			});
			//打印机版本获取回调
			globalEvent.addEventListener('onVersion', function(e) {
				// uni.showToast({
				// 	title: 'version: ' + JSON.stringify(e),
				// 	duration: 2000
				// });
			});
			
			globalEvent.addEventListener('getsupportprint', function(e) {
				console.log('key: ' + JSON.stringify(e));
				// uni.showToast({
				// 	title: 'key: ' + JSON.stringify(e),
				// 	duration: 2000
				// });
			});
			
			this.barcodeContent = option.barcodeContent;
			//显示加载框
			uni.showLoading({
				title: '加载中',
				mask:true
			});
			uni.request({
				url:this.baseUrl+'/system/record/barcode/boxs',
				data:{
					orgId:this.orgId,
					trojanCode:this.barcodeContent,
					userName:this.userName,
				},
				method: 'POST',
				header:{
					'authorization': getToken()
				},
				dataType:'json',
				success: (res) =>{
					console.log(res.data);
					if(res.data.code == '200'){
						this.productList = res.data.data.productList;
						var barList = res.data.data.barList;
						this.date = res.data.data.date;
						for(var i=0;i<barList.length;i++){
							var data = barList[i];
							this.message = this.message + data.moDocno+"-" + data.code+"-"+data.codeNum+"箱\n";
						}	
						//隐藏加载框
						uni.hideLoading();
					}else{
						//隐藏加载框
						uni.hideLoading();
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						});
					}
					
				}
			})
			uni.setNavigationBarTitle({
				title:'托码'+this.barcodeContent
			})
		},
		
		methods: {
			print(){
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask:true
				});
				var ret = printer.initPrinter({});
				printer.printEnableMark({
					enable: true
				});
				printer.setConcentration({ //设置打印浓度。font_level 取值范围：1~39。
					level: 39
				});
				printer.setLineSpacing({
					spacing: 1
				});
				printer.getsupportprint();
				
				printer.setFontSize({
					fontSize: 5
				});
				printer.setTextBold({
					bold: true
				});														
				printer.printBarcode({
					text: 'T'+this.barcodeContent,
					height: 50,
					barcodeType: 73,
					offset:1
				});
				printer.printText2({
					offset:1,
					fontSize:4,
					isBold:false,
					isUnderLine:false, 
					content:this.date+"\n",
				});
				printer.printText2({
					offset:1,
					fontSize:3,
					isBold:false,
					isUnderLine:false, 
					content:this.message,
				});
				printer.start();
				//隐藏加载框
				uni.hideLoading();
				setTimeout(function(){
					uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
					    delta: 1
					});
				},2000);	
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}
	.text-content {
		font-size: 48rpx;
		text-align: center;
	}
	
	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}
	
	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height:30px;
		font-size: 26rpx;
	}
	page {
		width: 100%;
		height: 100%;
	}
	
	
	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
	
	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}
	
	.list-cont {
		flex: 1;
		position: relative;
	
		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
	
		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;
	
		}
	}
	
	
	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>

